package com.bishe.tax.controller.sys;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.Taxpayer;
import com.bishe.tax.service.TaxpayerService;
import com.bishe.tax.service.ToDoaskService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 纳税人 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Controller
@RequestMapping("/system/taxpayer")
public class TaxpayerController {
	private String prefix = "taxpayer";

	@Autowired
	private TaxpayerService taxpayerService;
	@Autowired
	private ToDoaskService toDoaskService;

	@GetMapping("list")
	public String taxpayer() {
		return prefix + "/list";
	}

	/**
	 * 查询纳税人列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Taxpayer taxpayer,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		if (taxpayer.getStatus() == null) {
			taxpayer.setStatus(0);
		}
		List<Taxpayer> list = taxpayerService.selectTaxpayerList(taxpayer);
		return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
	}

	/**
	 * 查询纳税人列表
	 */
	@PostMapping("/trlist")
	@ResponseBody
	public TableDataInfo trlist(Taxpayer taxpayer,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<Taxpayer> list = taxpayerService.selectTaxpayerListByChoose(taxpayer);
		return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
	}

	/**
	 * 新增纳税人
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存纳税人
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Taxpayer taxpayer, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		taxpayer.setStatus(1);
		return AjaxResult.success(taxpayerService.insertTaxpayer(user, taxpayer));
	}

	/**
	 * 修改纳税人
	 */
	@GetMapping("/edit/{taxpayerId}")
	public String edit(@PathVariable("taxpayerId") Integer taxpayerId,
			@RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
		Taxpayer taxpayer = taxpayerService.selectTaxpayerById(taxpayerId);
		mmap.put("taxpayer", taxpayer);
		mmap.put("taskId", taskId);
		return prefix + "/edit";
	}

	/**
	 * 修改保存纳税人
	 */
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Taxpayer taxpayer, @RequestParam(value = "taskId", required = false) String taskId) {
		if (StringUtils.isNotBlank(taskId)) {
			toDoaskService.doComplete(taskId, null);
			taxpayer.setStatus(2);
		}
		return AjaxResult.success(taxpayerService.updateTaxpayer(taxpayer));
	}

	/**
	 * 删除纳税人
	 */
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		taxpayerService.deleteTaxpayerByIds(ids);
		return AjaxResult.success();
	}

	@GetMapping("examine/{taskId}/{taxpayerId}")
	public String doExamine(@PathVariable("taxpayerId") String taxpayerId, @PathVariable("taskId") String taskId,
			ModelMap mmap) {
		Taxpayer taxpayer = taxpayerService.selectTaxpayerById(Integer.valueOf(taxpayerId));
		mmap.put("taxpayer", taxpayer);
		mmap.put("taskId", taskId);
		return prefix + "/taxpayer";
	}

	/**
	 * 审核
	 *
	 * @param taskId
	 * @param flag
	 */
	@PostMapping("complete")
	@ResponseBody
	public AjaxResult doComplete(Taxpayer taxpayer, @RequestParam("taskId") String taskId,
			@RequestParam("flag") Boolean flag) {
		toDoaskService.doComplete(taskId, flag);
		if (flag) {
			taxpayer.setStatus(4);
		} else {
			taxpayer.setStatus(3);
		}
		return AjaxResult.success(taxpayerService.updateTaxpayer(taxpayer));
	}

	/**
	 * 提交审核
	 *
	 * @return
	 */
	@PostMapping("submitFlow")
	@ResponseBody
	public AjaxResult submitFlow(@RequestParam("taxpayerId") String taxpayerId, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		return AjaxResult.success(taxpayerService.doSubmitFlow(user, taxpayerId));
	}

	@PostMapping("selectTaxpayerByRiId")
	@ResponseBody
	public AjaxResult selectTaxpayerByRiId(@RequestParam("riId") String riId) {
		return AjaxResult.success(taxpayerService.selectTaxpayerByRiId(Integer.valueOf(riId)));
	}

}
